﻿using Blackthorn.Data.LinqToSql;
using Blackthorn.Data.LinqToSql.Repositories;
using Blackthorn.Data.Repository;
using Blackthorn.Data.UnitOfWork;
using Demo.Data.Model;
using Demo.LinqToSql.Data.Model;

namespace Demo.LinqToSql.Data
{
    public static class RepositoriesManager
    {
        private static readonly IDataUnitOfWork _unitOfWork;

        static RepositoriesManager()
        {
            _unitOfWork = new LinqToSqlUnitOfWork(new DataModelContext(@"Data Source = |DataDirectory|\LocalDatabase.sdf"));
            _unitOfWork.RegisterRepository<ICustomer, LinqToSqlRepository<ICustomer, Customer>>();
            _unitOfWork.RegisterRepository<IOrder, LinqToSqlRepository<IOrder, Order>>();
        }

        /// <summary>
        /// Gets Customers EntitiesRepository.
        /// </summary>
        public static IDataRepository<ICustomer> Customers
        {
            get { return _unitOfWork.GetRepository<ICustomer>(); }
        }

        /// <summary>
        /// Gets Orders EntitiesRepository.
        /// </summary>
        public static IDataRepository<ICustomer> Orders
        {
            get { return (IDataRepository<ICustomer>)_unitOfWork.GetRepository<IOrder>(); }
        }

        public static bool HasUnsavedChanges
        {
            get { return _unitOfWork.HasUnsavedChanges; }
        }

        public static void SaveChanges()
        {
           _unitOfWork.SaveChanges();
        }
    }
}
